﻿/*
 * Alunos: Thiago Augusto Pavan Rocha e Duyle Ruan Gonçalves de Campos
 * Matrícula: 00912326 e 00911960
 **/
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;

namespace PC
{
    class Xml
    {
        private string caminho = @"UserSettings.xml";
        private string defaultConfiguration = "#FFFFFF";
        private string NickName;

        public Xml(string NickName)
        {
            this.NickName = NickName;
            if (!File.Exists(caminho))
            {
                XmlDocument doc = new XmlDocument();
                XmlNode raiz = doc.CreateElement("Users");
                doc.AppendChild(raiz);
                doc.Save(caminho);
            }
        }

        private string Select()
        {
            int i;
            XmlDocument doc = new XmlDocument();
            String name;
            doc.Load(caminho);
            XmlNodeList nos;
            XmlNode no;
            nos = doc.SelectNodes("/Users/User");
            for (i = 0; i < nos.Count; i++)
            {
                no = nos.Item(i);
                name = no.SelectSingleNode("./Name").InnerText;
                if (name == this.NickName)
                {
                    return no.SelectSingleNode("./Configuration").InnerText;
                }
            }
            return "Novo";

        }

        private void Insert()
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(caminho);
            XmlNode linha = doc.CreateElement("User");
            XmlNode name = doc.CreateElement("Name");
            XmlNode configuration = doc.CreateElement("Configuration");

            name.InnerText = this.NickName;
            configuration.InnerText = this.defaultConfiguration; ;

            linha.AppendChild(name);
            linha.AppendChild(configuration);

            doc.SelectSingleNode("/Users").AppendChild(linha);

            doc.Save(caminho);
        }

        /* Retorna true caso for alterado e false caso não alterar, ele não ira altera caso não encontrar o usuario*/
        private Boolean Alter(string pConfiguration)
        {
            int i;
            XmlDocument doc = new XmlDocument();
            String name;
            doc.Load(caminho);
            XmlNodeList nos;
            XmlNode no;
            nos = doc.SelectNodes("/Users/User");
            for (i = 0; i < nos.Count; i++)
            {
                no = nos.Item(i);
                name = no.SelectSingleNode("./Name").InnerText;
                if (name == this.NickName)
                {
                    no.SelectSingleNode("./Configuration").InnerText = pConfiguration;
                    doc.Save(caminho);
                    return true;
                }
            }
            return false;
        }

        public string GetConfiguration()
        {
            string configuration;
            configuration = this.Select();
            if (configuration == "Novo")
            {
                this.Insert();

                return this.defaultConfiguration;
            }
            else {
                return configuration;
            }
        }

        public void setConfiguration(string pConfiguration)
        {
            bool alterado = this.Alter(pConfiguration);
            if (!alterado)
            {
                this.Insert();
            }
        }
    }
}
